# !/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName : zsj.py
# @Time     : 2024/4/13 19:09
# @Author   : Robot-Zsj
"""
description:

i:
6
1 -1 -6 7 -17 7
2

o:
14
"""
n = int(input())
scores = list(map(int, input().split()))
k = int(input())


def getResult():
    length = k + 1

    dp = [0] * n
    dp[0] = scores[0]

    queue = [dp[0]]

    for i in range(1, min(length, n)):

        dp[i] = queue[0] + scores[i]

        while len(queue) > 0 and dp[i] > queue[-1]:
            queue.pop()

        queue.append(dp[i])

    for i in range(length, n):
        if dp[i - length] == queue[0]:
            queue.pop(0)

        dp[i] = queue[0] + scores[i]

        while len(queue) > 0 and dp[i] > queue[-1]:
            queue.pop()

        queue.append(dp[i])

    return dp[n - 1]


print(getResult())
